﻿

import Request from '../../base/request.js?t=202511041006'
import Storage from '../../base/storage.js?t=202511041006'
import { IgetUserList, getBrowserInfo } from "../../base/utils.js?t=202511041006"
const Composition = (proxy) => {
    const data = reactive({
        proxy: "",
        messageTest: "",
        formState: {
            DepartmentName: "",
            ScoreRate: "",
            DepartmentTypeId: "",
        },
        deptList: [],
        userList: [
    
        ],
        pagination: {
            total: 0,
            loading: true,
            current: 1,
            pageSize: 50,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 50,
            // pageSizeOptions: ['5', '10', '15', '20'], // 可不设置使用默认
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗
        },



        rules: {
            DepartmentTypeId: [{ required: true, message: '请选择办公类型', trigger: 'change' }],
            ScoreRate: [{ required: true, message: '请填写扣分系数', trigger: 'change' }],

        },
        opType: "",

        t1: [],
        t2: [],
        cid:"",
        open: false,

        queryParams: {
            dept: null,
        },

        workTypeList: [


        ],

        columns: [
            {
                title: '序号',
                dataIndex: 'index',
                key: 'index',
                align: 'center',
                width: 80,
                customRender: ({  index }) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
                }
            },
            {
                title: '部门名称 ',
                dataIndex: 'DepartmentName',
                key: 'DepartmentName',
                align: 'center'

            },
            {
                title: '部门业务员',
                dataIndex: 'Type1List',
                key: 'Type1List',
                align: 'center',
                customRender: ({ text, record, index }) => {
                    if (text.length == 0) {
                        return '-'
                    } else {
                        let divcon = text && text.map(e => {
                            console.log(`${e.Email ? `${e.UserName}(${e.UserNo})[${e.Email || ''}]` : `${e.UserName}(${e.UserNo}) [暂无邮箱]`}`)
                            return h('div', {}, {
                                default: () => `${e.Email ? `${e.UserName}(${e.UserNo})[${e.Email || ''}]` : `${e.UserName}(${e.UserNo}) [暂无邮箱]`}`
                            })
                        })
                        return divcon || 'aaa'
                    }
     
           
                }
            },
            {
                title: '部门负责人',
                dataIndex: 'Type2List',
                key: 'Type2List',
                align: 'center',
                customRender: ({ text, record, index }) => {
                    if (text.length == 0) {
                        return '-'
                    } else {
                        let divcon = text && text.map(e => {
                            console.log(`${e.Email ? `${e.UserName}(${e.UserNo})[${e.Email || ''}]` : `${e.UserName}(${e.UserNo}) [暂无邮箱]`}`)
                            return h('div', {}, {
                                default: () => `${e.Email ? `${e.UserName}(${e.UserNo})[${e.Email || ''}]` : `${e.UserName}(${e.UserNo}) [暂无邮箱]`}`
                            })
                        })
                        return divcon || 'aaa'
                    }


                }
            },

            {
                title: '操作',
                key: 'action',
                align: 'center',
                customRender: function ({ text, record, index, column }) {
                    return h("div", {
                    },
                        [h(antd.Button, {
                            style: 'margin-right:4px',
                            type: "link",
                            onClick: () => { methods.handleDialog(1, record.Id) }
                        }, { default: () => '人员配置' })]
                    );



                }
            },
        ],
        dataSource: [],

    })
    let params = toRefs(data)

    const methods = {

        filterOption: (val, type) => {

            return `${type.label}`.includes(`${val || ''}`)
        },

        queryList: () => {

            methods.getTableList(true)

        },

        handleTableChange: (page) => {
            const { current, pageSize } = page
            params.pagination.value.current = current;
            params.pagination.value.pageSize = pageSize;
            methods.getTableList()
        },

        handleDialog: (type, id) => {

            params.cid.value = id
            params.pagination.value.loading = true
            Request({
                url: "/PPE/GetDepartmentConfigDetail",
                method: 'post',
                params: {
                    DepartmentId: id,
                },
            }).then(res => {

                console.log(res, 'res')
                params.t1.value = res.Data.Type1List.map(e => e.UserNo)

                params.t2.value = res.Data.Type2List.map(e => e.UserNo)

                params.formState.value.DepartmentName =res.Data.DepartmentName

             
                params.open.value = true
            }).finally(() => {
                params.pagination.value.loading = false
            })


        },
    
    

        getUserList: async () => {
            params.userList.value = await IgetUserList(Request)
        },

        getTableList: (isRresh) => {
            if (isRresh) {
                params.pagination.value.current = 1
            }
            params.pagination.value.loading = true
            Request({ 
                url: "/PPE/GetDepartmentConfigPageList",
                method: 'post',
                params: {
                    page: params.pagination.value.current,
                    pageSize: params.pagination.value.pageSize,
                    DepartmentId: params.queryParams.value.dept
                },
            }).then(res => {
                params.dataSource.value = res.Data.Data
                params.pagination.value.total = res.Data.Data.length
                params.pagination.value.loading = false

            })

        },


        init: () => {
              getBrowserInfo().catch(err => { console.log(err)})
            if (proxy) {
                params.proxy.value = proxy
            }
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount: 1,
                rtl: true,
            });

            let info = Storage.get('AccountInfo', true)
            Request({
                url: "/Account/createJwtToken",
                method: 'post',
                data: {
                    "userName": info && info.UserNo
                }
            }).then(res => {
                Storage.set('token', res.Data,true)
                methods.getTableList(true)

                methods.getUserList()
                Request({
                    url: "/SystemConfig/GetDepartmentList",
                    method: 'post',
                }).then(res => {
                    params.deptList.value = res.Data.map(e => {
                        return {
                            label: e.DepartmentName,
                            value: e.Id,

                        }
                    })

                })


            })

        },




        handleOk: () => {
            Request({
                url: "/PPE/EditDepartmentUser",
                method: 'post',
                data: {
                    DepartmentId: params.cid.value,
                    Type1List: params.t1.value,
                    Type2List: params.t2.value
                }
            }).then(res => {
                params.messageTest.value.success(res.Msg);
                methods.getTableList(true)
            }).finally(() => {
                params.open.value = false
                params.t1.value = []
                params.t2.value = []
            })
     
        }
    }
    const template = {
        test: {
            template: `<div @click="add">+</div> <div>{{testnum}}</div>`,

            setup() {

                return {
                    ...params,
                    ...methods,
                };
            }

        }
    }
    if (proxy) {

        params.proxy.value = proxy
    }

    //let template = toRefs(vtemplate)
    return { // 返回对象给模板

        ...params,
        ...methods,
        template

    }
}


export default Composition